package Designpatterns.shizhan.broker;

import java.lang.reflect.Proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JDKProxy implements InvocationHandler {
    private Object target ;
    private JDKProxy(Object target){
        this.target = target;
    }
    public static BusinessService createProxy(BusinessService target) {
        return (BusinessService) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new JDKProxy(target)
        );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //业务方法执行前的日志记录
        System.out.println("执行前日志");
        //调用真实的业务方法
        Object invoke = method.invoke(target,args);
        //业务方法执行后的日志记录
        System.out.println("执行后日志");
        return invoke;
    }
}
